package com.bitsbazaar.controller;

import java.sql.SQLException;
import java.util.Iterator;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.bitsbazaar.entity.Image;
import com.bitsbazaar.entity.Product;
import com.bitsbazaar.entity.User;
import com.bitsbazaar.manager.ImageManager;
import com.bitsbazaar.manager.ProductManager;
import com.bitsbazaar.manager.UserManager;

@Controller

public class BuyController {

	@Autowired
	private UserManager userManager;
	@Autowired
	private ProductManager productManager;
	@Autowired
	private ImageManager imageManager;
	
	@RequestMapping(value = "/buyPage", method = RequestMethod.GET)
	public ModelAndView contact() {
		String message = "Hello World, Spring 3.0 buyPage!";
		System.out.println(message);
		return new ModelAndView("buyPage", "message", message);
	}
	
	@RequestMapping(value = "/electronics", method = RequestMethod.GET)
	public ModelAndView electronics() {
		String message = "Hello World, Spring 3.0 electronics!";
		System.out.println(message);
		return new ModelAndView("electronics", "message", message);
	}
	
	@RequestMapping(value = "/getPhoto/{id}")
	public @ResponseBody byte[] getPhoto(@PathVariable("id") final long id)
	{
		System.out.println("Entering Controller");
		Image image = imageManager.getImageById(id);
		byte[] img = null;
		try {
			img = image.getImage().getBytes(1,(int) image.getImage().length());
		} catch (SQLException e) {
			System.out.println("");
			e.printStackTrace();
		}
		return img;
	}
	@RequestMapping(value = "/product_list/{id}", method = RequestMethod.GET)
	public ModelAndView productlisting(@PathVariable("id")final long id ) {
		System.out.println("Entered Ankits COntroller!" + id);
		 ModelAndView mav = new ModelAndView("product_list") ;
	        List<Product> products=productManager.getApprovedProductsBySubCat(id, 1);
	        System.out.println("products.size() : "+products.size());
	        mav.addObject("products",products);
	        return mav;
}
	
	@RequestMapping(value = "/product/{id}", method = RequestMethod.GET)
	public ModelAndView product(@PathVariable("id")final long id) {
		System.out.println("Entered Ankit's COntroller 2.0!" + id);
		
		
		 ModelAndView mav = new ModelAndView("product") ;
	        Product product=productManager.getProductById(id);
	        User user=userManager.getUserById(product.getUserId());
	        List<Image>  images=imageManager.getImageByParentId(id);
	        mav.addObject("product",product);
	        
	        mav.addObject("user",user);
	        
	        mav.addObject("image",(Image)images.get(0));
	        return mav;
	
	}
	@RequestMapping(value = "/product", method = RequestMethod.GET)
	public void ListProducts(){
		List<Product> products = productManager.getApprovedProducts(0);
		for(Iterator<Product> i = products.iterator(); i.hasNext();){
			Product item = i.next();
			System.out.println("Fetched new product id " + item.getId() + " name:" + item.getName());
		}
	}

}